package br.gov.ibama.client.visao;

import br.gov.ibama.client.AuthService;
import br.gov.ibama.client.AuthServiceAsync;
import br.gov.ibama.client.componentes.BasisComponentes;
import br.gov.ibama.client.comum.ConfiguradorDeMigalha;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.DialogBox;
import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.types.VerticalAlignment;
import com.smartgwt.client.types.VisibilityMode;
import com.smartgwt.client.widgets.Img;
import com.smartgwt.client.widgets.ImgButton;
import com.smartgwt.client.widgets.Label;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.LayoutSpacer;
import com.smartgwt.client.widgets.layout.SectionStack;
import com.smartgwt.client.widgets.layout.VLayout;

public class VisaoPrincipal extends HLayout {

	private static final int ALTURA_200 = 200;
	private static final String LOGO_IBAMA = "logo_prefeitura.jpg";
	private static final int HLAYOUT_3 = 3;
	private static final int ALTURA_60 = 60;
	private static final int LARGURA_20 = 20;
	private static final int LARGURA_250 = 250;
	private static final int MAX_ALTURA_400 = 400;
	private static final int SCROLL_BAR_SIZE_10 = 10;
	private static final int ALTURA_8 = 8;
	private static final String BORDER_1PX_SOLID_BBB = "1px solid #bbb";
	private static final String BACKGROUD_COLOR_EEE = "#eee";
	private static final String LARGURA_MIGALHA_60 = "60%";
	private VLayout vLayoutMenu;
	private SectionStack stackMenu;
	private ImgButton imgButtontHome;
	private ImgButton imgButtonAjuda;
	private ImgButton imgButtonLogout;
	private VLayout areaDeTrabalho;
	private ConfiguradorDeMigalha configuradorDeMigalha;

	public static final AuthServiceAsync authService = GWT
			.create(AuthService.class);
	
	public VisaoPrincipal() {
		setWidth100();
		setHeight100();
		setMembers(criaVLayoutMenu(), criaLayoutPrincipal());
	}

	private VLayout criaAreaDeTrabalho() {
		areaDeTrabalho = new VLayout();
		areaDeTrabalho.setWidth100();
		areaDeTrabalho.setHeight100();
		areaDeTrabalho.setShowResizeBar(Boolean.TRUE);
		return areaDeTrabalho;
	}

	private VLayout criaLayoutPrincipal() {
		VLayout vLayout = new VLayout();
		vLayout.setMembers(criaBarraSuperior(), criaAreaDeTrabalho());
		return vLayout;
	}

	private HLayout criaBarraSuperior() {
		Label labelMigalha = BasisComponentes.criaLabel("");
		labelMigalha.setWidth(LARGURA_MIGALHA_60);
		configuradorDeMigalha = new ConfiguradorDeMigalha(labelMigalha);
		criaBotoesDeControle();
		LayoutSpacer spacer = BasisComponentes.criaSpacer();
		spacer.setWidth(LARGURA_20);
		HLayout hLayoutBarraSuperior = new HLayout();
		hLayoutBarraSuperior.setWidth100();
		hLayoutBarraSuperior.setHeight(ALTURA_60);
		hLayoutBarraSuperior.setMembers(labelMigalha, spacer,
				criaBarraBotoesDeControle());
		hLayoutBarraSuperior.setPadding(2);
		hLayoutBarraSuperior.setBackgroundColor(BACKGROUD_COLOR_EEE);
		hLayoutBarraSuperior.setBorder(BORDER_1PX_SOLID_BBB);
		return hLayoutBarraSuperior;
	}

	private VLayout criaBarraBotoesDeControle() {
		LayoutSpacer spacerMargemSuperior = BasisComponentes.criaSpacer();
		spacerMargemSuperior.setHeight(ALTURA_8);
		HLayout botoesControle = new HLayout(HLAYOUT_3);
		botoesControle.setAlign(Alignment.RIGHT);
		botoesControle.setMembers(imgButtontHome, imgButtonAjuda,
				imgButtonLogout);
		VLayout botoesControleComEspacoSuperior = new VLayout();
		botoesControleComEspacoSuperior.setMembers(spacerMargemSuperior,
				botoesControle);
		return botoesControleComEspacoSuperior;
	}

	protected void criaBotoesDeControle() {
		imgButtontHome = BasisComponentes.criaBotaoControle("home_32x32.png",
				"Página principal");
		imgButtontHome.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				Window.Location.reload();
			}
		});
		imgButtonAjuda = BasisComponentes.criaBotaoControle("ajuda_32x32.png",
				"Ajuda");
		imgButtonLogout = BasisComponentes.criaBotaoControle(
				"logout_32x32.png", "Sair");
		imgButtonLogout.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				authService.logout(new AsyncCallback<Void>() {
					
					@Override
					public void onSuccess(Void result) {
						Window.Location.reload();
					}
					
					@Override
					public void onFailure(Throwable caught) {
						DialogBox mensagem = new DialogBox();
						mensagem.setText("Ops! Erro inesperado");
						
						mensagem.setWidget(new Label("Por favor, entre em contato com o suporte"));
						mensagem.show();
						mensagem.center();
						mensagem.setAutoHideEnabled(true);
					}
				});
			}
		});
	}

	private VLayout criaVLayoutMenu() {
		vLayoutMenu = new VLayout();
		vLayoutMenu.setWidth(LARGURA_250);
		vLayoutMenu.setHeight100();
		vLayoutMenu.setShowResizeBar(true);
		vLayoutMenu.setShowResizeBar(Boolean.TRUE);
		vLayoutMenu.setShowEdges(true);
		vLayoutMenu.setAlign(Alignment.CENTER);
		Img logo = new Img(LOGO_IBAMA, ALTURA_200, ALTURA_60);
		logo.setAlign(Alignment.CENTER);
		stackMenu = new SectionStack();
		stackMenu.setVisibilityMode(VisibilityMode.MUTEX);
		stackMenu.setScrollbarSize(SCROLL_BAR_SIZE_10);
		stackMenu.setScrollSectionIntoView(true);
		stackMenu.setMaxHeight(MAX_ALTURA_400);
		stackMenu.adjustForContent(true);
		stackMenu.setAlign(VerticalAlignment.TOP);
		vLayoutMenu.setMembers(logo, stackMenu);
		return vLayoutMenu;
	}

	public VLayout getvLayoutMenu() {
		return vLayoutMenu;
	}

	public void setvLayoutMenu(VLayout vLayoutMenu) {
		this.vLayoutMenu = vLayoutMenu;
	}

	public SectionStack getStackMenu() {
		return stackMenu;
	}

	public void setStackMenu(SectionStack stackMenu) {
		this.stackMenu = stackMenu;
	}

	public ImgButton getImgButtontHome() {
		return imgButtontHome;
	}

	public void setImgButtontHome(ImgButton imgButtontHome) {
		this.imgButtontHome = imgButtontHome;
	}

	public ImgButton getImgButtonAjuda() {
		return imgButtonAjuda;
	}

	public void setImgButtonAjuda(ImgButton imgButtonAjuda) {
		this.imgButtonAjuda = imgButtonAjuda;
	}

	public ImgButton getImgButtonLogout() {
		return imgButtonLogout;
	}

	public void setImgButtonLogout(ImgButton imgButtonLogout) {
		this.imgButtonLogout = imgButtonLogout;
	}

	public VLayout getAreaDeTrabalho() {
		return areaDeTrabalho;
	}

	public void setAreaDeTrabalho(VLayout areaDeTrabalho) {
		this.areaDeTrabalho = areaDeTrabalho;
	}

	public ConfiguradorDeMigalha getConfiguradorDeMigalha() {
		return configuradorDeMigalha;
	}
}
